<?php
/**
 * tour.php
 *
 * Tour类
 * @author konakona
 * @date 2009-9-28
 */
class tour {
	/**
	 * 添加Tour
	 * @param string $_post
	*/
	public static function tour_add($_post){
		if(strlen($_post['title'])<5){
			k::message('标题不能少于5个英文字符','javascript:history.go(-1);');
		}
		if(strlen($_post['itinerary'])<10){
			k::message('行程不能少于10个字符','javascript:history.go(-1);');
		}
		if(strlen($_post['special_features'])<10){
			k::message('特点不能少于10个字符','javascript:history.go(-1);');
		}
		if(strlen($_post['service_includes'])<10){
			k::message('包含服务不能少于10个字符','javascript:history.go(-1);');
		}
		if(strlen($_post['service_exclude'])<10){
			k::message('不包含服务不能少于10个字符','javascript:history.go(-1);');
		}
		$sql = "INSERT INTO tour values('','{$_post[district]}','{$_post[title]}','{$_post[itinerary]}','{$_post[special_features]}','{$_post[quotation]}','".time()."','{$_post[service_includes]}','{$_post[service_exclude]}','{$_post[attractions]}')";
		mysql::connect()->query($sql);
		k::message('成功添加新Tour!!','javascript:history.go(-1);');
	}
	/**
	 * 删除Tour
	 * @param mixed $id
	*/
	public static function tour_del($id){
                mysql::connect()->query("delete from tour_reviews where tid = $id");
		mysql::connect()->query("DELETE FROM tour WHERE id=$id");
		k::message('这条tour及它的反馈信息均已删除','javascript:history.go(-1);');
	}
	/**
	 * 列出所有Tour
	 * @param int $pid $dis_id
	 * @return array
	*/
	public static function tour_list($pid,$dis_name=false,$row=10){
		if($pid == 0){
			$pid = 1;
		}
		if($dis_name==false){
			if($pid==1){
				$sql = "select * from tour  ORDER BY id DESC limit ".($pid-1).",$row";
			}else{
				$page_start  = ($pid-1)+$row;
				$sql = "select * from tour  ORDER BY id DESC limit ".($page_start-1).",$row";
			}
		}else{
			if($pid == 1){
				$sql = "select district.name,tour.id,tour.attractions,tour.dis_id,tour.title,tour.itinerary,tour.special_features,tour.quotation,tour.addtime,tour.service_includes,tour.service_exclude from tour,district where district.name = '{$dis_name}' and tour.dis_id=district.id ORDER BY id DESC limit ".($pid-1).",$row";
			}else{
				$page_start  = ($pid-1)+$row;
				$sql = "select district.name,tour.id,tour.attractions,tour.dis_id,tour.title,tour.itinerary,tour.special_features,tour.quotation,tour.addtime,tour.service_includes,tour.service_exclude from tour,district where district.name = '{$dis_name}' and tour.dis_id=district.id ORDER BY id DESC limit ".($page_start-1).",$row";
			}
		}
		return mysql::connect()->fetch_all($sql);
	}
	/**
	 * 读取一条tour信息
	 * @param int $id
	 * @return array
	*/
	public static function tour_details($id){
		$sql = "select district.name,tour.id,tour.attractions,tour.dis_id,tour.title,tour.itinerary,tour.special_features,tour.quotation,tour.addtime,tour.service_includes,tour.service_exclude from tour,district where tour.id=$id and district.id = tour.dis_id";
		$result = mysql::connect()->fetch_all($sql);
		if($result[0]==''){
			k::message("Sorry,Can 't find No.{$id} tour.");
		}
		return $result[0];
	}
	/**
	 * 修改一条tour信息
	 * @param int $_post
	*/
	public static function tour_edit($_post){
		$sql = "UPDATE `tour` SET `title` = '{$_post[title]}', `itinerary` = '{$_post[itinerary]}',`dis_id` = '{$_post[district]}',`quotation`='{$_post[quotation]}',`service_includes`='{$_post[service_includes]}',`service_exclude`='{$_post[service_exclude]}',`special_features`='{$_post[special_features]}',`addtime`=".time().",`attractions`='{$_post[attractions]}' where id={$_post[id]}";
		mysql::connect()->query($sql);
		k::message('修改成功','?action=tour-list');
	}
	/**
	 * 获取一条tour的名称
	 * @param int $id
	*/
	public static function getTitle($id){
		$sql = "select title from tour where id=$id";
		$result = mysql::connect()->fetch_all($sql);
		return $result[0]['title'];
	}
}